﻿@using BDShop.Configuration
@using BDShop.Library.Extensions
@using BDShop.Utilities.Managers

@model PagedList.IPagedList<BDShop.Entities.ProductCacheComplex>
@using PagedList.Mvc;

@Html.Hidden("Url", (string)ViewBag.Url)
@Html.Hidden("Page", (int)ViewBag.page)
@Html.Hidden("Price", (string)ViewBag.Price)

<div class="product-filter">

    <div class="limit">
    </div>
    <div class="sort">
        <b>Hiển thị theo:</b>
        @Html.DropDownList("ddlSort", (IEnumerable<SelectListItem>)ViewBag.Sort, new { onchange = "Sort()" })
    </div>
</div>
<div class="product-compare"></div>
<!--Product Grid Start-->
<div class="product-grid">
    @foreach (var product in Model)
    {
        string[] categoryDefinition = null;
        if (!string.IsNullOrEmpty(product.CategoryDefinition))
        {
            categoryDefinition = product.CategoryDefinition.Split('|')[0].Split('#');
        }
        var url = UrlManager.ProductDetailUrl(product.ID, product.Name, categoryDefinition);
        <div>
            <div class="image">
                <a href="@url">
                    <img class="lazy" data-original="@Path.Combine(BDShopConfiguration.Storage.MainImagePath, product.MainImage)" src="@Path.Combine(BDShopConfiguration.Storage.MainImagePath, product.MainImage)" alt="@product.Name" title="@product.Name" style="width: 162px" /></a>
            </div>
            <div class="name"><a href="@url">@product.Name&quot;</a></div>
            <div class="description">@product.ShortDesc</div>
            <div class="price">
                @if (product.DiscountPrice == 0)
                {
                    <span class="price-new">@product.Price.ToCurrencyString(false, true)</span>
                }
                else
                {
                    <span class="price-old">@product.MarketPrice.ToCurrencyString(false, true)</span>
                    <br />
                    <span class="price-new">@product.Price.ToCurrencyString(false, true)</span>
                }
            </div>
            <div class="cart">
                <input type="button" value="Mua hàng" class="button" onclick="window.location.href='@url'" />
            </div>
            <div class="rating">
                @switch ((int)Math.Ceiling((decimal)(product.Rating / ((product.Rater == 0) ? 1 : product.Rater))))
                {
                    case 1:
                    <img src="/Images/stars-1.png" alt="1 sao." />
                        break;
                    case 2:
                    <img src="/Images/stars-2.png" alt="2 sao." />
                        break;
                    case 3:
                    <img src="/Images/stars-3.png" alt="3 sao." />
                        break;
                    case 4:
                    <img src="/Images/stars-4.png" alt="4 sao." />
                        break;
                    case 5:
                    <img src="/Images/stars-5.png" alt="5 sao." />
                        break;
                    default:
                        <img src="/Images/stars-1.png" alt="1 sao." />
                        break;
                }
            </div>
        </div>
    }
</div>
<br />
<!--Product Grid End-->
<!--Pagination Part Start-->
@if (Model.PageCount > 1)
{
    <div class="paging">
        <div class="links">
            @Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.sortOrder, price = ViewBag.Price, currentFilter = ViewBag.CurrentFilter }))
        </div>
        <div class="results">
            Trang @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) của @Model.PageCount
        </div>
    </div>
}
<!--Pagination Part End-->
<script>
    function Sort() {
        var url = $("#Url").val() + "?page=" + $("#Page").val() + "&sortOrder=" + $("#ddlSort option:selected").val() + "&price=" + $("#Price").val();
        window.location.href = url;
    }
</script>
